﻿using System;
using System.Windows.Controls;
using System.Windows.Threading;
using TheOliver.DigitalSignage.Client.Interfaces;
using System.ComponentModel;

namespace CeBIT2010
{
    public partial class CebitForeground : UserControl, IForeground
    {
        public CebitForeground()
        {
            InitializeComponent();

            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }

            //this.Loaded += CebitForeground_Loaded;
        }

        void CebitForeground_Loaded(object sender, System.Windows.RoutedEventArgs e1)
        {
            this.Loaded -= CebitForeground_Loaded;

            // Date Label Timer
            DispatcherTimer dayTimer = new DispatcherTimer();
            dayTimer.Tick += (s, e) =>
            {
                UpdateDateLabel();
            };
            dayTimer.Interval = new TimeSpan(0, 1, 0);
            dayTimer.Start();

            // Current and Next Session Timer
            DispatcherTimer sessionTimer = new DispatcherTimer();
            sessionTimer.Tick += (s, e) =>
            {
                UpdatePreview();
            };
            sessionTimer.Interval = new TimeSpan(0, 0, 5);
            sessionTimer.Start();

            UpdateDateLabel();
            UpdatePreview();
        }

        private void UpdatePreview()
        {
            Web.AllSlots nextSession = Configuration.NextSessionOfToday;
            Web.AllSlots currentSession = Configuration.CurrentSession;

            if (nextSession != null)
            {
                _nextSession.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                _nextSession.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (currentSession != null)
            {
                _currentSession.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                _currentSession.Visibility = System.Windows.Visibility.Collapsed;
            }

        }

        private void UpdateDateLabel()
        {
            _dateLabel.Text = Configuration.CurrentDateTime.Date.ToShortDateString();
        }
    }
}
